package com.weitu.threeSimultaneities.trackingMonthReport.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.threeSimultaneities.trackingMonthReport.entity.ReportInfo;
import com.weitu.threeSimultaneities.trackingMonthReport.service.ReportInfoService;
import com.weitu.threeSimultaneities.trackingMonthReport.service.ReportService;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * Created by WT-SUN on 2017/5/22.
 */
@Controller
@RequestMapping(value = "threeSimultaneities/report")
public class ReportController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "threeSimultaneities/trackingMonthReport/";

    @Autowired
    private ReportService reportService;

    @Autowired
    private ReportInfoService reportInfoService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return MODULE_BATH_PATH + "list";
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info() {
        return MODULE_BATH_PATH + "info";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit() {
        return MODULE_BATH_PATH + "edit";
    }

    @RequestMapping("reInfo")
    public ModelAndView reInfo(String id) {
        ModelAndView modelAndView = new ModelAndView();
        ReportInfo reportInfo =reportInfoService.selectByPrimaryKey(id);
        modelAndView.addObject("re", reportInfo);
        modelAndView.setViewName(MODULE_BATH_PATH + "reInfo");
        return modelAndView;
    }


    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return MODULE_BATH_PATH + "add";
    }

    @RequestMapping(value = "/addInfo", method = RequestMethod.GET)
    public String addInfo() {
        return MODULE_BATH_PATH + "addInfo";
    }

    @ResponseBody
    @RequestMapping("insertInfoSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "三同时——进度跟踪信息登记", methods = "添加月报明细")
    public AjaxResult insertInfoSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(paramMap);
        paramMap.put("delState",0);
        int boo = reportInfoService.insertSelective(paramMap);
        if (boo > 0) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }

    @ResponseBody
    @RequestMapping("insertSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "三同时——进度跟踪信息登记", methods = "添加月报")
    public AjaxResult insertSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(paramMap);
        String yearv=paramMap.get("year").toString();
        String monthv=paramMap.get("month").toString();
        String namev=yearv+"年"+monthv+"月"+"建设项目三同时整改跟踪月报";
        paramMap.put("name",namev);
        paramMap.put("delState",0);
        int boo = reportService.insertSelective(paramMap);
        if (boo > 0) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }

    /**
     * 月报列表
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/rTable", method = RequestMethod.POST)
    public Object rTable(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        int totalNum=reportService.selectReportCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,totalNum);
        PageView pageView=dataGridPagination.getPageView();
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);
        List<Map<String,Object>> maps=reportService.selectReport(paramMap);
        Object data=dataGridPagination.flushDataGridData(maps);
        return data;
    }

    /**
     * 月报明细列表
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/infoTb", method = RequestMethod.POST)
    public Object infoTb(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        int totalNum=reportInfoService.selectReportInfoCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,totalNum);
        PageView pageView=dataGridPagination.getPageView();
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);
        List<Map<String,Object>> maps=reportInfoService.selectReportInfo(paramMap);
        Object data=dataGridPagination.flushDataGridData(maps);
        return data;
    }

    @ResponseBody
    @RequestMapping("deleteById")
    @Transactional(readOnly = false)
    @SystemLog(module = "三同时——进度跟踪信息登记", methods = "删除月报")
    public AjaxResult deleteById(String id){
        int boo=reportService.deleteByPrimaryKey(id);
        if (boo > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }

    @ResponseBody
    @RequestMapping("deleteInfoReport")
    @Transactional(readOnly = false)
    @SystemLog(module = "三同时——进度跟踪信息登记", methods = "删除月报明细")
    public AjaxResult deleteInfoReport(String id){
        int boo=reportInfoService.deleteByPrimaryKey(id);
        if (boo > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }

    @ResponseBody
    @RequestMapping("updateReportState")
    @Transactional(readOnly = false)
    @SystemLog(module = "三同时——进度跟踪信息登记", methods = "提交月报明细")
    public AjaxResult updateReportState(String id){
        int boo=reportService.updateReportState(id);
        if (boo > 0) {
            return success("提交成功");
        } else {
            return fail("提交失败");
        }
    }
}
